﻿using System;

using Mvc.XForms.Utilities;
using StructureMap;
using Mvc.XForms.Examples.Repository;
using System.Web.Mvc;
using System.Reflection;

namespace Mvc.XForms.Examples {
	public class ExampleDeserializer : NameValueDeserializer {
		public ExampleDeserializer(ModelStateDictionary modelState)
			: base(modelState) { }

		protected override object GetEntity(long entityID, PropertyInfo property) {
			var repositoryType = typeof(IRepository<>).MakeGenericType(property.PropertyType);
			var getMethod = repositoryType.GetMethod("Get");

			// This would normally be a call to a service layer, but we only
			// have repositories for simplicity.
			var repos = ObjectFactory.GetInstance(repositoryType);
			var loadedEntity = getMethod.Invoke(repos, new object[] { entityID });

			return loadedEntity;
		}
	}
}
